package adapter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import com.actionbarsherlock.app.SherlockActivity;
import com.adgroup.smartnote.AcceptActivity;
import com.adgroup.smartnote.AddMemberToGroupActivity;
import com.adgroup.smartnote.CreateGroupActivity;
import com.adgroup.smartnote.LoginActivity;
import com.adgroup.smartnote.R;
import com.adgroup.smartnote.RegisterActivity;
import com.adgroup.smartnote.SearchFriend;
import com.adgroup.smartnote.SmartNoteActivity;
import com.androidquery.AQuery;
import com.fortysevendeg.swipelistview.SwipeListView;

import database.Database;

import JsonModel.User;
import Net.JsonParse;
import Utilities.Configuration;
import Utilities.Utility;
import ViewFragment.AcceptFriendActivity;
import adapter.AdapterCategories.Holder;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import animationmenu.CollapseAnimation;
import animationmenu.ExpandAnimation;

public class AdapterAcceptFriend {
	ArrayList<User> arr;
	AcceptFriendActivity c;
	public AdapterAcceptFriend(AcceptFriendActivity acceptFriendActivity, ArrayList<User> arr)
	{
		this.c = acceptFriendActivity;
		this.arr = arr;
	}
	public class Holder
	{
		ImageView iv;
		TextView tv;
		TextView tvEmail;
		LinearLayout root;
		ImageView btn;
		Button btnAccept, btnDeny;
	}
	public class Adapter extends BaseAdapter 
	{

		@Override
		public int getCount() {
			return arr.size();
		}

		@Override
		public Object getItem(int arg0) {
			return arr.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}
		int pointed = 0;
		public User getUser()
		{
			return arr.get(pointed);
		}
		public void setArrayUser(ArrayList<User> a){
			arr = a;
		}
		@Override
		public View getView(final int arg0, View arg1, ViewGroup arg2) {
			Holder holder;
			LayoutInflater inflater = (LayoutInflater) c.getSherlockActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			if (arg1 == null) {
				holder = new Holder();
				arg1 = inflater.inflate(R.layout.itemacceptdeny,arg2, false);
				holder.tv = (TextView) arg1
						.findViewById(R.id.textViewNicknameAcceptDeny);
				holder.iv = (ImageView) arg1
						.findViewById(R.id.imageViewLogoAcceptDeny);	
				holder.tvEmail = (TextView) arg1.findViewById(R.id.textViewEmailAcceptDeny);
				holder.btn = (ImageView) arg1.findViewById(R.id.imageviewAcceptDeny);
				holder.btnAccept = (Button) arg1.findViewById(R.id.buttonAcceptAcceptDeny);
				holder.btnDeny = (Button) arg1.findViewById(R.id.buttonDenyAcceptDeny);
				holder.root = (LinearLayout) arg1.findViewById(R.id.front);
				arg1.setTag(holder);
			} else {
				holder = (Holder) arg1.getTag();
			}
		//	((SwipeListView)arg1).recycle(arg1, arg0);
			//get model friend
			final User f = arr.get(arg0);
			holder.tv.setText(f.getNickName());
			holder.tvEmail.setText(f.getTime());
						
			AQuery aq = new AQuery(arg1);
			Bitmap placeholder = aq.getCachedImage(R.drawable.imageload);
			aq.id(holder.iv).image(arr.get(arg0).getLink(), true,
						true, 0, 0, placeholder, AQuery.FADE_IN_NETWORK, 1.0f);
			
			
			holder.btn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View view) {
					c.updateAnimationItem(arg0);
				}
			});
			holder.btnAccept.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg) {
					c.sendRequestAcceptMakeFriend(arr.get(arg0),1,"self",c.getSherlockActivity());
				}
			});
			holder.btnDeny.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					c.sendRequestAcceptMakeFriend(arr.get(arg0),0,"self",c.getSherlockActivity());
				}
			});
			holder.root.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg) {
					Log.d("log","asd");
					((AcceptActivity)c.getSherlockActivity()).ShowSlidingMenu(arr.get(arg0));
				}
			});
			
			return arg1; 
		}

		public void removeUser(User u) {
			arr.remove(u);
			this.notifyDataSetChanged();
		}

		
	}
	
	//cancel make friend			
	class AsynCancelRequest extends AsyncTask<String, Void, String>
	{
		ProgressDialog progress;
		String nickname = "";
		int index;
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			progress = ProgressDialog.show(c.getSherlockActivity(), "",
					c.getResources().getString(R.string.titledialogcancelmakefriend), true);
		}
		@Override
		protected String doInBackground(String... params) {
			nickname = params[2];
			try {
				index = Integer.parseInt(params[3]);
			} catch (Exception e) {
				index = -1;
			}
			
			return new JsonParse().sendCancelMakeFriend(c.getSherlockActivity(), params);
		}
		@Override
		protected void onPostExecute(String result) {
			Utility.ShowAlert(c.getSherlockActivity(), c.getResources().getString(R.string.buttonmakefriendrequested), c.getResources().getString(R.string.messagemakefriendrequested)+" "+nickname);
			
			super.onPostExecute(result);
		}
	}
}
